import bb.cascades 1.2
import "controls"
import "lib"
import bb.device 1.2
//import bb.com 1.1

Page {
    property string baseUrl: "http://m.scykw.cn/api/ifengnews/"
    signal registed();
    
    titleBar: TitleBar {
        title: qsTr("Register")
    }

    function getPostData(opt,pin) {
        var postdata = [];
        postdata.push("token=" + _Setting.getToken());
        postdata.push("softname=ifengNews");
        postdata.push("pin=" + pin);
        var m = _Setting.getEncrypt(postdata.join(""));
        postdata.push("m=" + m);
        postdata.push("t=" + fun.formatDate("yyyyMMDDHHmmSS"));
        postdata.push("opt=" + opt);

        return postdata.join("&")
    }

    ScrollView {
        Container {
            attachedObjects: [
                HardwareInfo {
                    id: hardwareinfo
                },
                Common {
                    id: fun
                }
            ]

            Header {
                title: qsTr("No.1")
            }

            Container {
                leftPadding: 10
                rightPadding: leftPadding
                topPadding: leftPadding
                bottomPadding: leftPadding

                Label {
                    multiline: true
                    text: qsTr("Pay treasure to the developer (snajlqw@126.com) into the registration fee 10 yuan, at the same time, note the following (ifengNews, PIN:%1).").arg(hardwareinfo.pin) + Retranslate.onLocaleOrLanguageChanged
                }
            }

            Header {
                title: qsTr("No.2")
            }

            Container {
                Divider {
                    topMargin: 0
                    bottomMargin: 0
                    opacity: 0.0
                }

                leftPadding: 10
                rightPadding: leftPadding
                topPadding: leftPadding
                bottomPadding: leftPadding

                Container {
                    Label {
                        multiline: true
                        text: qsTr("To submit application for registration to the registration server.")
                    }
                }

                Padding {
                    horizontalAlignment: HorizontalAlignment.Center
                    WaitButton {
                        text: qsTr("Application for registration")
                        loading: false
                        //发送注册申请
                        onClick: {
                            var postStr = getPostData("insert",hardwareinfo.pin)
                            console.log(postStr);
                            loading = true;
                            fun.post(baseUrl, postStr, function(data) {
                                    loading = false;
                                    console.log(data.message)
                                    if (data.status) {
                                        var json = JSON.parse(data.message)
                                        _Setting.alert(json.message)
                                    } else {
                                        _Setting.alert(data.message)
                                    }
                                });
                        }
                    }
                }
            }

            Header {
                title: qsTr("No.3")
            }

            Container {
                leftPadding: 10
                rightPadding: leftPadding
                topPadding: leftPadding
                bottomPadding: leftPadding

                Divider {
                    topMargin: 0
                    bottomMargin: 0
                    opacity: 0.0
                }
                Container {
                    Label {
                        multiline: true
                        text: qsTr("Contact the registration server, check the registration status, to complete the final registration.")
                        //联系注册服务器，查看注册状态，完成最终注册。
                    }
                }

                Padding {
                    horizontalAlignment: HorizontalAlignment.Center
                    WaitButton {
                        text: qsTr("Verification of registration")
                        loading: false
                        onClick: {
                            var postStr = getPostData("isRegist",hardwareinfo.pin)
                            console.log(postStr);
                            loading = true;
                            fun.post(baseUrl, postStr, function(data) {
                                    loading = false;
                                    if (data.status) {
                                        var json = JSON.parse(data.message)
                                        if(json.status == "1"){
                                            _Setting.setReg("1");
                                            registed();
                                        }
                                        _Setting.alert(json.message)
                                    } else {
                                        _Setting.alert(data.message)
                                    }
                            });
                        }
                    }
                }
            }
        }
    }
}
